package com.fowo.api.syncAmazon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.entity.SellOrder;
import com.fowo.api.mapper.SellOrderMapper;
import com.fowo.api.syncAmazon.api.OrderService;
import com.fowo.api.syncAmazon.entity.TmpSellOrder;
import com.fowo.api.syncAmazon.entity.TmpSellOrderDetail;
import com.fowo.api.syncAmazon.mapper.TmpSellOrderMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@DS("ds24")
public class TmpSellOrderServiceImpl
        extends ServiceImpl<TmpSellOrderMapper, TmpSellOrder>{

    @Resource
    private OrderService orderService;
    @Resource
    private TmpSellOrderMapper tmpSellOrderMapper;
    @Resource
    private SellOrderMapper sellOrderMapper;
    @Resource
    private TmpSellOrderDetailServiceImpl tmpSellOrderDetailService;

    public void syncOrderList() throws Exception {
        List<TmpSellOrder> tmpSellOrderList = orderService.getOrder();
        if(CollectionUtil.isNotEmpty(tmpSellOrderList)){
            List<TmpSellOrderDetail> tmpSellOrderDetailList = orderService.getOrderDetails(tmpSellOrderList);
            save(tmpSellOrderList);
            saveDetail(tmpSellOrderDetailList);
        }
    }

    public void save(List<TmpSellOrder> tmpSellOrderList){
        if(tmpSellOrderList == null || tmpSellOrderList.size() == 0){
            return;
        }

        tmpSellOrderMapper.deleteAll();
        List<String> amazonOrderIds = tmpSellOrderList.stream().map(t -> t.getAmazonOrderId()).collect(Collectors.toList());
        List<SellOrder> oldSellOrders = sellOrderMapper.selectList(Wrappers.lambdaQuery(SellOrder.class).in(SellOrder::getAmazonOrderId, amazonOrderIds));
        List<TmpSellOrder> list = compareNewOrOldData(tmpSellOrderList,oldSellOrders);
//        if(this.saveBatch(list)){
//            tmpSellOrderMapper.updateToSellOrder();
//            tmpSellOrderMapper.insertToSellOrder();
//        }
        //分批次插入
        List<List<TmpSellOrder>> partition = Lists.partition(list, 200);
        for (List<TmpSellOrder> orders:partition) {
            if(this.saveBatch(orders)){
                tmpSellOrderMapper.updateToSellOrder();
                tmpSellOrderMapper.insertToSellOrder();
            }
        }
    }

    /**
     * 比较新老数据
     * @param newOrders
     * @param oldOrders
     * @return
     */
    public List<TmpSellOrder> compareNewOrOldData(List<TmpSellOrder> newOrders,List<SellOrder> oldOrders){
        List<TmpSellOrder> result = new ArrayList<>();
        while(!newOrders.isEmpty()){
            TmpSellOrder newOrder = newOrders.get(0);
            newOrders.remove(newOrder);

            SellOrder oldOrder = oldOrders.stream().filter(o -> o.getAmazonOrderId().equals(newOrder.getAmazonOrderId())).findFirst().orElse(null);

            if(null == oldOrder){
                newOrder.setIsUpdate(false);
                result.add(newOrder);
                continue;
            }

            oldOrders.remove(oldOrder);
            // 如果订单更新时间相同，那么数据就不用处理直接跳过
            if(newOrder.getLastUpdateDate().equals(oldOrder.getLastUpdateDate())){
                continue;
            }
            newOrder.setIsUpdate(true);
            result.add(newOrder);

        }
        return result;
    }

    public void saveDetail(List<TmpSellOrderDetail> tmpSellOrderDetailList){
        if(tmpSellOrderDetailList == null || tmpSellOrderDetailList.size() == 0){
            return;
        }
        tmpSellOrderMapper.deleteDetailAll();
        List<List<TmpSellOrderDetail>> partition = Lists.partition(tmpSellOrderDetailList, 200);
        for (List<TmpSellOrderDetail> list:partition) {
            if(tmpSellOrderDetailService.saveBatch(list)){
                tmpSellOrderMapper.insertOrderDetail();
                tmpSellOrderMapper.updateOrderDetail();
            }
        }
    }
}
